
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import org.hibernate.Transaction;
import org.hibernate.Session;
public class RunFrame extends javax.swing.JFrame 
{
    private ArrayList<String> selected_type_list = new ArrayList<String>();
    public RunFrame() 
    {
        initComponents();
        createComboBoxList();
    }
    public void LocateFrameToTheCenterOfTheScreen()
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize(); 
        Dimension framesize = this.getSize();
        int x = (int) ( screenSize.getWidth() - framesize.getWidth() ) / 2;
        int y = (int) ( screenSize.getHeight() - framesize.getHeight() ) / 2;
        setLocation( x, y );
        setResizable(false);
        setVisible( true );
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        button_add_files = new javax.swing.JButton();
        label_count = new javax.swing.JLabel();
        label_language = new javax.swing.JLabel();
        combo_box_language = new javax.swing.JComboBox();
        label_type = new javax.swing.JLabel();
        label_size = new javax.swing.JLabel();
        check_box_txt = new javax.swing.JCheckBox();
        check_box_jpg = new javax.swing.JCheckBox();
        check_box_png = new javax.swing.JCheckBox();
        check_box_html = new javax.swing.JCheckBox();
        text_field_count = new javax.swing.JTextField();
        text_field_size = new javax.swing.JTextField();
        button_clear_table = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        button_add_files.setText("Add Files To Table");
        button_add_files.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_add_filesActionPerformed(evt);
            }
        });

        label_count.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        label_count.setText("Count:");

        label_language.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_language.setText("Language:");

        combo_box_language.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_box_languageActionPerformed(evt);
            }
        });

        label_type.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_type.setText("Type:");

        label_size.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label_size.setText("Size (bytes):");

        check_box_txt.setText("txt");
        check_box_txt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                check_box_txtActionPerformed(evt);
            }
        });

        check_box_jpg.setText("jpg");
        check_box_jpg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                check_box_jpgActionPerformed(evt);
            }
        });

        check_box_png.setText("png");
        check_box_png.setActionCommand("jCheckBox3");
        check_box_png.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                check_box_pngActionPerformed(evt);
            }
        });

        check_box_html.setText("html");
        check_box_html.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                check_box_htmlActionPerformed(evt);
            }
        });

        text_field_count.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        text_field_count.setText("10");
        text_field_count.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                text_field_countActionPerformed(evt);
            }
        });

        text_field_size.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        text_field_size.setText("100");
        text_field_size.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                text_field_sizeActionPerformed(evt);
            }
        });

        button_clear_table.setText("ClearTable");
        button_clear_table.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_clear_tableActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(check_box_txt)
                        .addGap(18, 18, 18)
                        .addComponent(check_box_html)
                        .addGap(18, 18, 18)
                        .addComponent(check_box_png)
                        .addGap(18, 18, 18)
                        .addComponent(check_box_jpg))
                    .addComponent(label_type)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(label_size, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(label_language)
                            .addComponent(label_count, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(combo_box_language, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(text_field_size, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(text_field_count, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(button_add_files)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_clear_table, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(label_language, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(combo_box_language, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(text_field_count, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_count, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_size, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(text_field_size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_type, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(check_box_txt)
                    .addComponent(check_box_html)
                    .addComponent(check_box_png)
                    .addComponent(check_box_jpg))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_add_files)
                    .addComponent(button_clear_table))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void button_add_filesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_add_filesActionPerformed
    String language = getLanguage();   
    int count = getFilesCount();
    int size = getFilesSize();

    Manager manager = new Manager(language);
    manager.addFileByCount(selected_type_list, count, size);
}//GEN-LAST:event_button_add_filesActionPerformed
private String getLanguage()
{
    return combo_box_language.getSelectedItem().toString();
}
private int getFilesCount()
{
    return Integer.parseInt(text_field_count.getText());
}
private int getFilesSize()
{
    return Integer.parseInt(text_field_size.getText());
}

private void combo_box_languageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_box_languageActionPerformed

}//GEN-LAST:event_combo_box_languageActionPerformed

private void check_box_txtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_check_box_txtActionPerformed
    if(check_box_txt.isSelected())
    {
        selected_type_list.add("txt");
    }
    else if(!check_box_txt.isSelected())
    {
        selected_type_list.remove("txt");
    }
}//GEN-LAST:event_check_box_txtActionPerformed

private void text_field_countActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_text_field_countActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_text_field_countActionPerformed

private void text_field_sizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_text_field_sizeActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_text_field_sizeActionPerformed

private void check_box_htmlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_check_box_htmlActionPerformed
    if(check_box_html.isSelected())
    {
        selected_type_list.add("html");
    }
    else if(!check_box_html.isSelected())
    {
        selected_type_list.remove("html");
    }
}//GEN-LAST:event_check_box_htmlActionPerformed

private void check_box_pngActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_check_box_pngActionPerformed
    if(check_box_png.isSelected())
    {
        selected_type_list.add("png");
    }
    else if(!check_box_png.isSelected())
    {
        selected_type_list.remove("png");
    }
}//GEN-LAST:event_check_box_pngActionPerformed

private void check_box_jpgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_check_box_jpgActionPerformed
    if(check_box_jpg.isSelected())
    {
        selected_type_list.add("jpg");
    }
    else if(!check_box_jpg.isSelected())
    {
        selected_type_list.remove("jpg");
    }
}//GEN-LAST:event_check_box_jpgActionPerformed

private void button_clear_tableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_clear_tableActionPerformed
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "delete from TableFile";
        session.createQuery(query).executeUpdate();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
}//GEN-LAST:event_button_clear_tableActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_add_files;
    private javax.swing.JButton button_clear_table;
    private javax.swing.JCheckBox check_box_html;
    private javax.swing.JCheckBox check_box_jpg;
    private javax.swing.JCheckBox check_box_png;
    private javax.swing.JCheckBox check_box_txt;
    private javax.swing.JComboBox combo_box_language;
    private javax.swing.JLabel label_count;
    private javax.swing.JLabel label_language;
    private javax.swing.JLabel label_size;
    private javax.swing.JLabel label_type;
    private javax.swing.JTextField text_field_count;
    private javax.swing.JTextField text_field_size;
    // End of variables declaration//GEN-END:variables

    private void createComboBoxList()
    {
        PageList page_list = new PageList();
        for(int i = 0; i < page_list.getLanguageList().length; i++)
            combo_box_language.addItem(page_list.getLanguageList()[i]);
    }     
}
